<?php
/*
 * @Author: lokei
 * @Date: 2022-08-31 15:47:43
 * @LastEditors: lokei
 * @LastEditTime: 2022-10-22 09:24:31
 * @Description: 
 */

namespace App\Http\Controllers\Iot\Gateway;

use App\Common\Tools\ResultTool;
use App\Common\Tools\Sn;
use App\Http\Controllers\Controller;
use App\Models\Iot\Gateway\GatewayModel;
use Illuminate\Http\Request;

class GatewayController extends Controller
{
    public function list()
    {
        $items = GatewayModel::get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function get(Request $request)
    {
        $gateway = GatewayModel::where('id', '=', $request->input('id'))->first();
        $res = ResultTool::success();
        $res['data'] = $gateway;
        return $res;
    }

    public function create(Request $request)
    {
        $gateway = new GatewayModel();
        $gateway->identity = Sn::build_order_no();
        $gateway->name = $request->input('name');
        $gateway->ip = $request->input('ip');
        $gateway->save();
        return ResultTool::success();
    }

    public function update(Request $request)
    {
        $gateway = GatewayModel::where('id', '=', $request->input('id'))->first();
        $gateway->name = $request->input('name');
        $gateway->ip = $request->input('ip');
        $gateway->save();
        return ResultTool::success();
    }

    public function delete(Request $request)
    {
        GatewayModel::where('id', '=', $request->input('id'))->delete();
        return ResultTool::success();
    }
}
